package com.alexander.imgbannerwheelview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;


/**
 * @author Sigthp-P-Department
 * @date 2018/9/7
 */

public class CustomRoundAngleImageView extends AppCompatImageView
{
    float width, height;
    float mDefaultRoundAngle = 12;
    private float leftTopRoundAngle;
    private float leftBottomRoundAngle;
    private float rightTopRoundAngle;
    private float rightBottomRoundAngle;
    private Path path;

    public CustomRoundAngleImageView(Context context, float[] radius)
    {
        this(context, null, radius);
    }

    public CustomRoundAngleImageView(Context context, AttributeSet attrs, float[] radius)
    {
        this(context, attrs, 0, radius);
    }

    public CustomRoundAngleImageView(Context context, AttributeSet attrs, int defStyleAttr, float[] radius)
    {
        super(context, attrs, defStyleAttr);
        init(context, radius);
    }

    private void init(Context context, float[] radius)
    {
        if (Build.VERSION.SDK_INT < 18)
        {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        path = new Path();
        leftTopRoundAngle = radius[0];
        rightTopRoundAngle = radius[1];
        rightBottomRoundAngle = radius[2];
        leftBottomRoundAngle = radius[3];
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom)
    {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        if (width >= 12 && height > 12)
        {
            canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
            //四个圆角
            path.moveTo(leftTopRoundAngle, 0);
            path.lineTo(width - rightTopRoundAngle, 0);
            path.quadTo(width, 0, width, rightTopRoundAngle);
            path.lineTo(width, height - rightBottomRoundAngle);
            path.quadTo(width, height, width - rightBottomRoundAngle, height);
            path.lineTo(leftBottomRoundAngle, height);
            path.quadTo(0, height, 0, height - leftBottomRoundAngle);
            path.lineTo(0, leftTopRoundAngle);
            path.quadTo(0, 0, leftTopRoundAngle, 0);

            canvas.clipPath(path);
        }
        super.onDraw(canvas);
    }

}
